
package vistas;

import controladores.CProducto;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import modelos.MTabla;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VProducto extends javax.swing.JFrame {

    private Vector<JTextField> edit;
    private Vector<JButton> boton;
    private CProducto control;
    private MTabla modeloTabla;
    private Vector<String> fila;
    private AuxVista auxvista;


    public VProducto() {
        initComponents();
        modeloTabla= new MTabla();
        auxvista = new AuxVista();
        control= new CProducto(this);
        fila= new Vector<String>();
        edit = new Vector<JTextField>();
        boton = new Vector<JButton>();
        
        tblIngrediente.setModel(modeloTabla);
        fila.add("Ingrediente");
        fila.add("Porcion");
        modeloTabla.setColumnIdentifiers(fila);
        
        boton.add(btnSalir);
        boton.add(btnConsultar);
        boton.add(btnRegistrar);
        boton.add(btnCancelar);
        boton.add(btnEliminar);
        boton.add(btnQuitar);
        boton.add(btnAgregar);
       
        edit.add(txtCodigo);
        edit.add(txtDescripcion);
        edit.add(txtRacion);
        edit.add(txtPrecio);
        
        for (int i = 0; i < boton.size(); i++)
            boton.elementAt(i).addActionListener(control);
        reiniciar();
    }

    public boolean validarCamposVacios(){
        return auxvista.validarCamposVacios(edit);
    }

    public boolean validarNumeros(){
        for (int i = 2; i < edit.size(); i++)
            try{
                 if(Double.parseDouble(edit.elementAt(i).getText())<=0){
                    JOptionPane.showMessageDialog(new JFrame(),"Lo informacion ingresada debe ser numeros validos por favor cambielos","Comida Rapida",JOptionPane.WARNING_MESSAGE);
                    return false;
                 }
            }catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(new JFrame(),"Lo informacion ingresada debe ser numeros validos por favor cambielos","Comida Rapida",JOptionPane.WARNING_MESSAGE);
                return false;
            }

        return true;
    }

    public void bloquear(){
        txtCodigo.setEditable(false);
        txtRacion.setEditable(false);
        btnAgregar.setEnabled(false);
        cmbIngredientes.setEnabled(false);
        btnQuitar.setEnabled(false);
    }

    public boolean actualizar(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()))){
               modeloTabla.setValueAt(txtRacion.getText(), i, 1);
               return true;
            }
        return false;
    }


    public void agregarCategoria(String dato){
        cmbCategoria.setSelectedItem(dato);
    }

    public void agregarFila(String [] v){
        if(!actualizar()){
                fila= new Vector<String>();
                fila.add(v[0]);
                fila.add(v[1]);
                modeloTabla.addRow(fila);
            }
    }

     public void agregarFila1(String [] v){
        fila= new Vector<String>();
        fila.add(v[0]);
        fila.add(v[1]);
        modeloTabla.addRow(fila);
    }

    public void quitar(){
        int i = tblIngrediente.getSelectedRow();
        if(i != -1)
            modeloTabla.removeRow(i);
        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar el regitstro que desea quitar","Comida Rapida",JOptionPane.ERROR_MESSAGE);
    }

     public void reiniciar(){
        auxvista.reiniciar(edit);
        txtCodigo.setEditable(true);
        txtRacion.setEditable(true);
        btnAgregar.setEnabled(true);
        btnQuitar.setEnabled(true);
        cmbIngredientes.setEnabled(true);
        modeloTabla.setRowCount(0);
     }

    public void cargarComboIngrediente(Vector<String> descripciones){
        cmbIngredientes.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbIngredientes.addItem(descripciones.elementAt(i));

        }
    }
    
    public void cargarComboCategoria(Vector<String> descripciones){
        cmbCategoria.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbCategoria.addItem(descripciones.elementAt(i));

        }
    }

    public JTable getTblIngrediente() {
        return tblIngrediente;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtDescripcion() {
        return txtDescripcion;
    }

    public JTextField getTxtPorcion() {
        return txtRacion;
    }

    public JTextField getTxtPrecio() {
        return txtPrecio;
    }

    public String getTextoCmbIngrediente() {
        return (String) cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex());
    }

    public String getTextoCmbCategoria() {
        return (String) cmbCategoria.getItemAt(cmbCategoria.getSelectedIndex());
    }

     public Vector<String []> getInfoTabla(){
        Vector<String []> v = new Vector<String[]>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++){
            String [] s = new String [] {""+modeloTabla.getValueAt(i, 0),""+modeloTabla.getValueAt(i, 1)};
            v.add(s);
         }
         return v;
    }

    public String [] getRegistro(){
        String [] s = new String [] {""+cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()),txtRacion.getText()};

         return s;
    }

     

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblIngrediente = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        btnRegistrar = new javax.swing.JButton("Registrar");
        btnConsultar = new javax.swing.JButton("Consultar");
        btnCancelar = new javax.swing.JButton("Cancelar");
        btnSalir = new javax.swing.JButton("Salir");
        btnEliminar = new javax.swing.JButton("Eliminar");
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblIngrediente1 = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cmbCategoria = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPrecio = new javax.swing.JTextField();
        txtDescripcion = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        cmbIngredientes = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        txtRacion = new javax.swing.JTextField();
        btnAgregar = new javax.swing.JButton("Agregar");
        btnQuitar = new javax.swing.JButton("Quitar");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Productos");
        getContentPane().setLayout(null);

        tblIngrediente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblIngrediente);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(30, 250, 800, 160);
        jScrollPane1.setBackground(new java.awt.Color(255,255,255));

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(null);

        btnRegistrar.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnRegistrar.setText("Registrar");
        btnRegistrar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnRegistrar);
        btnRegistrar.setBounds(30, 20, 120, 30);

        btnConsultar.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnConsultar.setText("Consultar");
        jPanel3.add(btnConsultar);
        btnConsultar.setBounds(160, 20, 120, 30);

        btnCancelar.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnCancelar.setText("Cancelar");
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnCancelar);
        btnCancelar.setBounds(30, 100, 120, 30);

        btnSalir.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnSalir.setText("Salir");
        jPanel3.add(btnSalir);
        btnSalir.setBounds(160, 100, 120, 30);

        btnEliminar.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnEliminar.setText("Eliminar");
        jPanel3.add(btnEliminar);
        btnEliminar.setBounds(100, 60, 120, 30);

        getContentPane().add(jPanel3);
        jPanel3.setBounds(280, 440, 310, 150);
        jPanel3.setBackground(new java.awt.Color(255,255,255));

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setLayout(null);

        tblIngrediente1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblIngrediente1);

        jPanel1.add(jScrollPane2);
        jScrollPane2.setBounds(20, 470, 650, 160);

        jLabel11.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel11.setText("Ingredientes");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(420, 270, 110, 30);

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        jPanel1.add(txtCodigo);
        txtCodigo.setBounds(140, 50, 190, 30);

        jLabel6.setText("Codigo:");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(20, 50, 130, 30);

        jLabel1.setText("Categoria:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 10, 140, 32);

        jPanel1.add(cmbCategoria);
        cmbCategoria.setBounds(140, 10, 190, 30);
        cmbCategoria.setBackground(new java.awt.Color(14,151,255));

        jLabel2.setText("Descripcion:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(20, 90, 140, 32);

        jLabel3.setText("Precio:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(20, 130, 224, 32);

        txtPrecio.setFont(new java.awt.Font("Arial", 0, 13));
        jPanel1.add(txtPrecio);
        txtPrecio.setBounds(140, 130, 190, 30);

        txtDescripcion.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        jPanel1.add(txtDescripcion);
        txtDescripcion.setBounds(140, 90, 190, 32);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(30, 50, 360, 180);
        jPanel1.setBackground(new java.awt.Color(255,255,255));

        jLabel10.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel10.setText("Producto");
        getContentPane().add(jLabel10);
        jLabel10.setBounds(160, 10, 80, 30);

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel4.setLayout(null);

        jPanel4.add(cmbIngredientes);
        cmbIngredientes.setBounds(10, 30, 170, 30);
        cmbIngredientes.setBackground(new java.awt.Color(14,151,255));

        jLabel5.setText("Racion:");
        jPanel4.add(jLabel5);
        jLabel5.setBounds(200, 30, 70, 30);

        txtRacion.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        jPanel4.add(txtRacion);
        txtRacion.setBounds(290, 30, 110, 30);

        btnAgregar.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        btnAgregar.setText("Agregar");
        jPanel4.add(btnAgregar);
        btnAgregar.setBounds(70, 80, 120, 30);

        btnQuitar.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        btnQuitar.setText("Quitar");
        jPanel4.add(btnQuitar);
        btnQuitar.setBounds(230, 80, 120, 30);

        getContentPane().add(jPanel4);
        jPanel4.setBounds(400, 50, 430, 180);
        jPanel4.setBackground(new java.awt.Color(255,255,255));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds(0, 0, 855, 639);
        getContentPane().setBackground(new java.awt.Color(14,151,255));
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodigoActionPerformed


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VProducto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnQuitar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbCategoria;
    private javax.swing.JComboBox cmbIngredientes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblIngrediente;
    private javax.swing.JTable tblIngrediente1;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtPrecio;
    private javax.swing.JTextField txtRacion;
    // End of variables declaration//GEN-END:variables



}
